package com.zhu.controller.system;

import com.alibaba.fastjson.JSON;
import com.zhu.annotate.Export;
import com.zhu.pojo.Role;
import com.zhu.query.RoleQuery;
import com.zhu.service.IRoleService;
import com.zhu.utils.ExcelUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author xiaozhu
 * @since 2022-09-03
 */
@RestController
@RequestMapping("/role")
public class RoleController {

    @Autowired
    private IRoleService roleService;

    @Autowired
    private ExcelUtils excelUtils;

    /*
        查询所有的角色信息
     */
    @RequestMapping("/all")
    public List<Role> getRole(RoleQuery roleQuery){
        return roleService.getRole(roleQuery);
    }

    /*
        根据id查找对应的角色信息
     */
    @RequestMapping("/detail")
    public Role getRoleDetail(@RequestParam("roleId") Integer roleId){
        return roleService.getById(roleId);
    }

    /*
        添加或修改对应角色信息
     */
    @PostMapping("/action")
    public String actionRole(@RequestBody Role role){
        if(role.getRoleId()!=null){
            roleService.updateById(role);
        }else{
            roleService.save(role);
        }
        return "ok";
    }

    /*
        删除对应角色信息
     */
    @PostMapping("/del")
    public String delRole(@RequestBody List<Integer> selectionList){
        roleService.removeBatchByIds(selectionList);
        return "ok";
    }

    @Export
    @PostMapping("/export")
    public void export(@RequestBody List<Role> roles, HttpServletResponse response) throws IOException {
        excelUtils.export(roles,Role.class,response);
    }

}
